package com.spring.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring.springboot.param.UserParam;
import com.spring.springboot.query.UserQuery;
import com.spring.springboot.result.UserResult;
import com.spring.springboot.service.UserService;
import com.spring.springboot.sys.core.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * 用户(User)表控制层
 *
 * @author 宋义州
 * @since 2022-07-23 01:41:43
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {

    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增__User__", notes = "新增一个__User__")
    @Operation(security = {@SecurityRequirement(name = "Authorization")})
    @ApiImplicitParam(name = "user", value = "User", required = true, paramType = "body", dataType = "UserParam", dataTypeClass = UserParam.class)
    public Result insert(@RequestBody UserParam user) {
        this.userService.insertSelective(user);
        return Result.success("新增成功！");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除__User__", notes = "根据id删除一个__User__")
    @Operation(security = {@SecurityRequirement(name = "Authorization")})
    @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    public Result delete(@PathVariable Long id) {
        this.userService.deleteByPrimaryKey(id);
        return Result.success("删除成功！");
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新__User__", notes = "更新一个__User__")
    @Operation(security = {@SecurityRequirement(name = "Authorization")})
    @ApiImplicitParam(name = "user", value = "User", required = true, paramType = "body", dataType = "UserParam", dataTypeClass = UserParam.class)
    public Result update(@RequestBody UserParam user) {
        this.userService.updateByPrimaryKeySelective(user);
        return Result.success("更新成功！");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取__User__", notes = "根据id获取一个__User__")
    @Operation(security = {@SecurityRequirement(name = "Authorization")})
    @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    public Result detail(@PathVariable Long id) {
        UserResult result = this.userService.selectByPrimaryKey(id);
        return Result.success("查询成功！", result);
    }

    /**
     * 分页查询所有数据
     *
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页获取__User__", notes = "分页获取__User__列表")
    @Operation(security = {@SecurityRequirement(name = "Authorization")})
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页条数", required = true, paramType = "query", dataType = "int", dataTypeClass = Integer.class)
    }
    )
    public Result list(@ApiIgnore UserQuery user) {
        Page<UserResult> userPage = this.userService.selectByPage(user);
        return Result.success("查询成功！", userPage);
    }

}

